package cn.hl.hm.util.paging;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xianghan
 * 
 *         分页对象定义 2016年4月12日下午2:17:49
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class PageInfo implements Serializable {

	private static final long serialVersionUID = 587754556498974978L;

	// 每一页记录数
	@JsonProperty("pageCount")
	private int pageSize = 10;

	// 记录总的页数
	@JsonProperty("totlePage")
	private int totalPage;

	// 总记录数
	@JsonProperty("totleCount")
	private int totalResult;

	// 当前页数
	@JsonProperty("pageId")
	private int currentPage;

	// 当前显示到的ID, 在mysql limit 中就是第一个参数.
	@JsonIgnore
	private int currentResult;
	@JsonIgnore
	private String sortField;
	@JsonIgnore
	private String order;
	@JsonIgnore
	private List<Object> dataList;
	@JsonIgnore
	private Map<String, Object> params = new HashMap<String, Object>();// 其他的参数我们把它分装成一个Map对象

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalResult() {
		return totalResult;
	}

	public void setTotalResult(int totalResult) {
		this.totalResult = totalResult;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getCurrentResult() {
		return currentResult;
	}

	public void setCurrentResult(int currentResult) {
		this.currentResult = currentResult;
	}

	public String getSortField() {
		return sortField;
	}

	public void setSortField(String sortField) {
		this.sortField = sortField;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	// 得到最后一页；
	@JsonIgnore
	public int getLastPage() {

		int lastPage = 0;
		if (totalResult % pageSize == 0) {
			lastPage = totalResult / pageSize;
		} else {
			lastPage = totalResult / pageSize + 1;
		}

		return lastPage;
	}

	public Map<String, Object> getParams() {
		return params;
	}

	public void setParams(Map<String, Object> params) {
		this.params = params;
	}

	@SuppressWarnings("rawtypes")
	public List getDataList() {
		return dataList;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setDataList(List dataList) {
		this.dataList = dataList;
	}

	public static String getStringForIn(List list){
		if(list!=null && list.size()>0){
			return list.toString().replace("[", "(").replace("]", ")");
		}
		return "";
	}

	public PageInfo build(){
		int totalResult = this.getTotalResult();   //总记录数
		int totalPage=totalResult%pageSize==0?totalResult/pageSize:totalResult/pageSize+1;  //记录总的页数
		setTotalResult(totalResult);
		setTotalPage(totalPage);
		if (getCurrentPage()<=0){
			setCurrentPage(1);
		}
		return this;
	}

}
